package kalender.backend;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;


public class Luser {

	private int uid;
	private String uname;
	private String passwd;
	private String name;
    private String cg_class;
    private String cg_program;
	
	public int getUid() { return uid; }
	public void setUid(int id) { uid = id; }
	
	public String getUname() { return uname; }
	public void setUname(String un) { uname = un; }
	
	public String getPasswd() { return passwd; }
	public void setPasswd(String p) { passwd = p; }
	
	public String getName() { return name; }
	public void setName(String n) { name = n; }
    
    public String getCg_class() { return cg_class; }
    public void setCg_class(String c) { cg_class = c; }
    
    public String getCg_program() { return cg_program; }
    public void setCg_program(String pr) { cg_program = pr; }
	
	public String toString() {
		return "uid: " + getUid() + " uname: " + getUname() + " passwd: " + getPasswd() + " name: " + getName();
	}
	
	public static Luser find(int uid) throws BackendException {
		Connection conn = DatabaseManager.getConnection();
		try {
			PreparedStatement ps = conn.prepareStatement("select * from luser where uid = ?");
			ps.setInt(1, uid);
			ResultSet rs = ps.executeQuery();
			SQLWarning warn1 = ps.getWarnings();
			DatabaseManager.printWarning(warn1);
			SQLWarning warn2 = rs.getWarnings();
			DatabaseManager.printWarning(warn2);
			if (rs.next()) {
				Luser l = new Luser();
				l.name = rs.getString("name");
				l.passwd = rs.getString("passwd");
				l.uid = uid;
				l.uname = rs.getString("uname");
                l.cg_class = rs.getString("cg_class");
                l.cg_program = rs.getString("cg_program");
				return l;
			} else {
				return null;
			}
		} catch (SQLException e) {
			DatabaseManager.printException(e);
			throw new BackendException("Databasfel. Var god försök igen.", e);
		}
	}
	

	public static Luser find(String username) throws BackendException {
		Connection conn = DatabaseManager.getConnection();
		try {
			PreparedStatement ps = conn.prepareStatement("select * from luser where uname = ?");
			ps.setString(1, username);
			ResultSet rs = ps.executeQuery();
			SQLWarning warning = ps.getWarnings();
			DatabaseManager.printWarning(warning);
			SQLWarning warn2 = rs.getWarnings();
			DatabaseManager.printWarning(warn2);
			
			if (rs.next()) {
				Luser l = new Luser();
				l.name = rs.getString("name");
				l.passwd = rs.getString("passwd");
				l.uid = rs.getInt("uid");
				l.uname = rs.getString("uname");
                l.cg_class = rs.getString("cg_class");
                l.cg_program = rs.getString("cg_program");
				return l;
			} else {
				return null;
			}
		} catch (SQLException e) {
			DatabaseManager.printException(e);
			throw new BackendException("Databasfel. Var god försök igen.", e);
		}
	}

	public static List<Luser> findAll() throws BackendException {
		Connection conn = DatabaseManager.getConnection();
		try {
			PreparedStatement ps = conn.prepareStatement("select * from luser order by uid");
			ResultSet rs = ps.executeQuery();
			SQLWarning warning = ps.getWarnings();
			DatabaseManager.printWarning(warning);
			SQLWarning warn2 = rs.getWarnings();
			DatabaseManager.printWarning(warn2);
			
			ArrayList<Luser> users = new ArrayList<Luser>();
			while (rs.next()) {
				Luser l = new Luser();
				l.name = rs.getString("name");
				l.passwd = rs.getString("passwd");
				l.uid = rs.getInt("uid");
				l.uname = rs.getString("uname");
                l.cg_class = rs.getString("cg_class");
                l.cg_program = rs.getString("cg_program");
				users.add(l);
			}
			return users;
		} catch (SQLException e) {
			DatabaseManager.printException(e);
			throw new BackendException("Databasfel. Var god försök igen.", e);
		}
	}
	
	public void saveNew() throws BackendException {
		Connection conn = DatabaseManager.getConnection();
		try {
			PreparedStatement ps = conn.prepareStatement("insert into luser (uname, passwd, name, cg_class, cg_program) values(?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS);
			ps.setString(1, uname);
			ps.setString(2, passwd);
			ps.setString(3, name);
            ps.setString(4, cg_class);
            ps.setString(5, cg_program);
			ps.executeUpdate();
			SQLWarning warning = ps.getWarnings();
			DatabaseManager.printWarning(warning);
			ResultSet keys = ps.getGeneratedKeys();
			keys.next();
			uid = keys.getInt(1);
		} catch (SQLException e) {
			DatabaseManager.printException(e);
			throw new BackendException("Databasfel. Var god försök igen.", e);
		}
	}
	
	public void saveUpdate() throws BackendException {
		Connection conn = DatabaseManager.getConnection();
		try {
			PreparedStatement ps = conn.prepareStatement("update luser set uname = ?, passwd = ?, name = ?, cg_class = ?, cg_program = ? where uid = ?");
			ps.setString(1, uname);
			ps.setString(2, passwd);
			ps.setString(3, name);
            ps.setString(4, cg_class);
            ps.setString(5, cg_program);
			ps.setInt(6, uid);
			ps.executeUpdate();
			SQLWarning warning = ps.getWarnings();
			DatabaseManager.printWarning(warning);
		} catch (SQLException e) {
			DatabaseManager.printException(e);
			throw new BackendException("Databasfel. Var god försök igen.", e);
		}
	}
	
	public static void delete(int uid) throws BackendException {
		Connection conn = DatabaseManager.getConnection();
		try {
			PreparedStatement ps = conn.prepareStatement("delete from luser where uid = ?");
			ps.setInt(1, uid);
			ps.executeUpdate();
			SQLWarning warning = ps.getWarnings();
			DatabaseManager.printWarning(warning);
		} catch (SQLException e) {
			DatabaseManager.printException(e);
			throw new BackendException("Databasfel. Var god försök igen.", e);
		}
	}
}
